home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / cug231 / cldict.c < prev    next >
Text File  |  1987-06-17  |  4KB  |  136 lines

  1. /*
  2.     Little Smalltalk
  3.         Internal class dictionary
  4.  
  5.         timothy a. budd, 10/84
  6.  
  7.     In order to facilitate lookup, classes are kept in an internal data
  8.     dictionary.  Classes are inserted into this dictionary using a
  9.     primtitive, and are removed by either being overridden, or being
  10.     flushed at the end of execution.
  11. */
  12. /*
  13.     The source code for the Little Smalltalk System may be freely
  14.     copied provided that the source of all files is acknowledged
  15.     and that this condition is copied with each file.
  16.  
  17.     The Little Smalltalk System is distributed without responsibility
  18.     for the performance of the program and without any guarantee of
  19.     maintenance.
  20.  
  21.     All questions concerning Little Smalltalk should be addressed to:
  22.  
  23.         Professor Tim Budd
  24.         Department of Computer Science
  25.         Oregon State University
  26.         Corvallis, Oregon
  27.         97331
  28.         USA
  29. */
  30. # include <stdio.h>
  31. # include "object.h"
  32. # include "number.h"
  33. # include "symbol.h"
  34. # include "primitive.h"
  35.  
  36. struct class_entry {        /* structure for internal dictionary */
  37.     char *cl_name;
  38.     object *cl_description;
  39.     struct class_entry *cl_link;
  40.     };
  41.  
  42. static struct class_entry *class_dictionary = 0;
  43. int ca_cdict = 0;
  44. static mstruct *fr_cdict = 0;        /* class dictionary free list */
  45.  
  46. # define CDICTINIT 30
  47. static struct class_entry cdsinit[CDICTINIT];
  48.  
  49. /* cdic_init - initialize the internal class dictionary */
  50. cdic_init() {
  51.     struct class_entry *p;
  52.     mstruct *new;
  53.     int i;
  54.  
  55.     for (p = cdsinit, i = 0; i < CDICTINIT; i++, p++) {
  56.         new = (mstruct *) p;
  57.         new->mlink = fr_cdict;
  58.         fr_cdict = new;
  59.         }
  60. }
  61.  
  62. /* enter_class - enter a class into the internal class dictionary */
  63. enter_class(name, description)
  64. char *name;
  65. object *description;
  66. {    struct class_entry *p;
  67.  
  68.     for (p = class_dictionary; p; p = p->cl_link)
  69.         if (strcmp(name, p->cl_name) == 0) {
  70.             assign(p->cl_description, description);
  71.             return;
  72.             }
  73.     /* not found, make a new entry */
  74.     if (fr_cdict) {
  75.         p = (struct class_entry *) fr_cdict;
  76.         fr_cdict = fr_cdict->mlink;
  77.         }
  78.     else {
  79.         p = structalloc(struct class_entry);
  80.         ca_cdict++;
  81.         }
  82.     p->cl_name = name;
  83.     sassign(p->cl_description, description);
  84.     p->cl_link = class_dictionary;
  85.     class_dictionary = p;
  86. }
  87.  
  88. /* lookup - take a name and find the associated class object */
  89. object *lookup_class(name)
  90. char *name;
  91. {    struct class_entry *p;
  92.  
  93.     for (p = class_dictionary; p; p = p->cl_link)
  94.         if (strcmp(name, p->cl_name) == 0)
  95.             return(p->cl_description);
  96.     return((object *) 0);
  97. }
  98.  
  99. /* free_all_classes - flush all references for the class dictionary */
  100. free_all_classes()
  101. {    struct class_entry *p;
  102.  
  103.     for (p = class_dictionary; p; p = p->cl_link) {
  104.         obj_dec(p->cl_description);
  105.         }
  106. }
  107.  
  108. /* class_list - list all the subclasses of a class (recursively),
  109.     indenting by a specified number of tab stops */
  110. class_list(c, n)
  111. class *c;
  112. int n;
  113. {    struct class_entry *p;
  114.     object *prs[2];
  115.     class *q;
  116.     char *name;
  117.  
  118.     /* first print out this class name */
  119.     if (! is_symbol(c->class_name))
  120.         return;
  121.     sassign(prs[0], c->class_name);
  122.     name = symbol_value(c->class_name);
  123.     sassign(prs[1], new_int(n));
  124.     primitive(SYMPRINT, 2, prs);
  125.     obj_dec(prs[0]);
  126.     obj_dec(prs[1]);
  127.  
  128.     /* now find all subclasses and print them out */
  129.     for (p = class_dictionary; p; p = p->cl_link) {
  130.         q = (class *) p->cl_description;
  131.         if ((is_symbol(q->super_class)) &&
  132.            (strcmp(name, symbol_value(q->super_class)) == 0) )
  133.             class_list(q, n+1);
  134.         }
  135. }
  136.